package demo.database.mssql;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.titan.core.utils.data.FormatUtils;
import com.titan.core.utils.db.DBUtils;
import com.titan.core.utils.db.RecordSet;
import com.titan.core.utils.dbinfo.dialect.DBDialect;
import com.titan.core.utils.dbinfo.dialect.IDBDialect;

/**
 *
 *
 * @author 周成功
 * 
 */
public class ShowTableRows {
    public static void main(String[] args) {
        String[][] config = new String[][] {
//          {"oracle", "192.168.128.212", "1521", "ORCL", "hrbsgts", "hrbsgts"}
//          {"mssql", "192.168.128.213", "1433", "H20170913", "sa", "123456"}
            {"mysql", "127.0.0.1", "3306", "member", "root", "123456"}
        };
        
        try {
            for (int i = 0; i < config.length; i++) {
                IDBDialect dialect = null;
                if("oracle".equalsIgnoreCase(config[i][0])) {
                    dialect = DBDialect.ORACLE.get();
                } else if("mysql".equalsIgnoreCase(config[i][0])) {
                    dialect = DBDialect.MYSQL.get();
                } else if("mssql".equalsIgnoreCase(config[i][0])) {
                    dialect = DBDialect.MSSQL.get();
                } else {
                    throw new RuntimeException("不知道的数据库类型["+config[i][0]+"]！");
                }
                
                Connection conn = null;
                List<String> tables = null;
                try {
                    conn = dialect.getConnection(config[i][1], Integer.parseInt(config[i][2]), config[i][3], config[i][4], config[i][5]);
                    tables = dialect.getTables(conn, config[i][3]);
                    for (int j = 0; j < tables.size(); j++) {
                        String tablename = tables.get(j);
                        String sql = "select count(*) from " + dialect.identifier(tablename);
                        RecordSet rest = null;
                        try {
                            rest = DBUtils.executeQueryToRecordSet(conn, sql);
                            if(rest.moveNext()) {
                                String count = rest.getString(1);
                                String text = "["+FormatUtils.to2Digit(j+1)+"/"+FormatUtils.to2Digit(tables.size())+"]"+tablename+"\t\t"+count;
                                tables.set(j, text);
                            }                            
                        } finally {
                            DBUtils.close(rest);
                        }
                    }
                } finally {
                    DBUtils.close(conn);
                }
                for (int j = 0; j < tables.size(); j++) {
                    System.out.println(tables.get(j));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

